package vashion.azeroth.back.controller.item;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.contant.ForestErrorEnum;
import vashion.azeroth.contant.ItemErrorEnum;
import vashion.azeroth.core.item.pojo.ForestPropertyDO;
import vashion.azeroth.core.item.pojo.ForestPropertyValueDO;
import vashion.azeroth.core.item.pojo.PropertyMetaDO;
import vashion.azeroth.core.manager.forest.BaseForestPropertyManager;
import vashion.azeroth.core.manager.forest.BasePropertyManager;
import vashion.azeroth.core.result.ApiResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类目属性维护
 *
 * @param <T>
 */
@Controller
@RequestMapping("/back/forestproperty")
public class BackForestPropertyController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(BackForestPropertyController.class);

	@Autowired
	private BaseForestPropertyManager baseForestPropertyManager;
	@Autowired
	private BasePropertyManager basePropertyManager;

	@RequestMapping("publishForestProperty.htm")
	@ResponseBody
	public ApiResult<Boolean> publishForestProperty(
			@ModelAttribute("forestPropertyDO") ForestPropertyDO forestPropertyDO, HttpServletRequest request) {

		ApiResult<Boolean> publishResult = baseForestPropertyManager.publishForestProperty(forestPropertyDO,
				getLoginUserId(request));
		if (publishResult.isSuccess() && publishResult.getData()) {
			return ApiResult.newSuccessResult();
		} else {
			return ApiResult.newErrorResult(publishResult.getErrorCode(), publishResult.getErrorMsg());
		}
	}

	@RequestMapping("publishForestProperties.htm")
	@ResponseBody
	public ApiResult<Boolean> publishForestProperties(@RequestParam(value = "forestId") long forestId,
			@RequestParam(value = "propertyIds") String propertyIds, HttpServletRequest request) {
		try {
			String[] propertyIdArr = propertyIds.split(",");
			ForestPropertyDO forestPropertyDO = new ForestPropertyDO();
			forestPropertyDO.setForestId(forestId);
			for (String propertyIdStr : propertyIdArr) {
				long propertyId = NumberUtils.toLong(propertyIdStr);
				forestPropertyDO.setPropertyId(propertyId);
				// 取出title
				PropertyMetaDO propertyMetaDO = basePropertyManager.get(propertyId);
				forestPropertyDO.setPropertyTitle(propertyMetaDO.getTitle());
				forestPropertyDO.setPropertyType(propertyMetaDO.getPropertyType());
				ApiResult<Boolean> publishResult = baseForestPropertyManager.publishForestProperty(forestPropertyDO,
						getLoginUserId(request));
				if (!publishResult.isSuccess() || !publishResult.getData()) {
					return ApiResult.newErrorResult(ForestErrorEnum.SYSTEM_ERROR.getErrorCode(),
							ForestErrorEnum.SYSTEM_ERROR.getErrorMessage());
				}
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(ForestErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ForestErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return ApiResult.newSuccessResult(true);
	}

	@RequestMapping("publishPropertiesToForest.htm")
	@ResponseBody
	public ApiResult<Boolean> publishPropertiesToForest(
			@ModelAttribute("forestPropertyDO") ForestPropertyDO forestPropertyDO,
			@RequestParam(value = "propertyValues") String propertyValues, HttpServletRequest request) {
		ApiResult<Boolean> publishResult = baseForestPropertyManager.publishPropertiesToForest(forestPropertyDO,
				propertyValues, getLoginUserId(request));
		if (publishResult.isSuccess() && publishResult.getData()) {
			return ApiResult.newSuccessResult();
		} else {
			return ApiResult.newErrorResult(publishResult.getErrorCode(), publishResult.getErrorMsg());
		}
	}

	/**
	 * 获取某一个类目的属性列表，可按照属性value查询
	 *
	 * @return
	 */
	@RequestMapping("getPropertiesForForest.htm")
	@ResponseBody
	public ApiResult<List<ForestPropertyValueDO>> getPropertiesForForest(@ModelAttribute("forestPropertyDO") ForestPropertyDO forestPropertyDO, 
																		String propertyValue) {

		forestPropertyDO.setFirstRow((forestPropertyDO.getPageNum() - 1) * forestPropertyDO.getPageSize());
		return baseForestPropertyManager.getPropertiesForForest(forestPropertyDO, propertyValue);
	}

	/**
	 * 获取属性列表
	 *
	 * @return
	 */
	@RequestMapping("getForestPropertyList.htm")
	@ResponseBody
	public ApiResult<List<ForestPropertyDO>> getForestPropertyList(long forestId) {

		return baseForestPropertyManager.getForestPropertyList(forestId, null);
	}

	/**
	 * 删除类目属性
	 *
	 * @return
	 */
	@RequestMapping("remove.htm")
	@ResponseBody
	public ApiResult<Boolean> remove(@ModelAttribute("forestPropertyDO") ForestPropertyDO forestPropertyDO,
			HttpServletRequest request) {

		int removeCount = baseForestPropertyManager.operateRemove(forestPropertyDO, getLoginUserId(request));
		if (removeCount > 0) {
			return ApiResult.newSuccessResult(true);
		} else {
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

}
